package com.xiaoyushu.note.biz.rpc;

import com.xiaoyushu.framework.common.response.Response;
import com.xiaoyushu.kv.dto.api.KeyValueFeignApi;
import com.xiaoyushu.kv.dto.req.AddNoteContentReqDTO;
import com.xiaoyushu.kv.dto.req.DeleteNoteContentReqDTO;
import com.xiaoyushu.kv.dto.req.FindNoteContentReqDTO;
import com.xiaoyushu.kv.dto.rsp.FindNoteContentRspDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Objects;

@Component
public class KeyValueRpcService {

    @Resource
   private KeyValueFeignApi keyValueFeignApi;
    @Qualifier("resourceHandlerMapping")
    @Autowired
    private HandlerMapping resourceHandlerMapping;

    public boolean saveNoteContent(String uuid, String content){
        AddNoteContentReqDTO addNoteContentReqDTO = new AddNoteContentReqDTO();
        addNoteContentReqDTO.setUuid(uuid);
        addNoteContentReqDTO.setContent(content);
        Response<?> response = keyValueFeignApi.addNoteContent(addNoteContentReqDTO);

        if ((Objects.isNull( response)|| !response.isSuccess())){
            return false;
        }
        return true;
    }

    public boolean deleteNoteContent(String uuid){

        DeleteNoteContentReqDTO deleteNoteContentReqDTO = new DeleteNoteContentReqDTO();
        deleteNoteContentReqDTO.setUuid(uuid);

        Response<?> response = keyValueFeignApi.deleteNoteContent(deleteNoteContentReqDTO);

        if ((Objects.isNull( response)|| !response.isSuccess())){
            return false;
        }
        return true;
    }


    public String findNoteContent(String uuid){
        FindNoteContentReqDTO findNoteContentReqDTO = new FindNoteContentReqDTO();
        findNoteContentReqDTO.setUuid(uuid);
        Response<FindNoteContentRspDTO> noteContent = keyValueFeignApi.findNoteContent(findNoteContentReqDTO);
        if (Objects.isNull( noteContent)|| !noteContent.isSuccess()|| Objects.isNull(noteContent.getData())){
            return null;
        }

        return noteContent.getData().getContent();
    }

}
